package cn.quang.search.admin.entity.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

/**
 * 含分页信息的列表
 */
@Setter
@Getter
public class Pagination<T> extends ArrayList<T> {
    /**
     * 每页数据数
     */
    private int pageSize;

    /**
     * 当前页码（base on 1)
     */
    private int pageIndex;

    private long totalCount;

    /**
     * 当前页返回的数据条数
     */
    public int getCurrentPageRowCount(){
        return this.size();
    }

    /**
     * 总页数
     */
    public long getPageCount(){
        if(totalCount<1 || pageSize<1){
            return 0;
        }

        return (totalCount + pageSize - 1)/pageSize;
    }

    public Pagination(){}

    public Pagination(int pageIndex, int pageSize){
        this(pageIndex, pageSize, 0L);
    }

    public Pagination(int pageIndex, int pageSize, long totalCount){
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }

    public Pagination(Pagination<?> page){
        if(page != null) {
            this.pageIndex = page.getPageIndex();
            this.pageSize = page.getPageSize();
            this.totalCount = page.getTotalCount();
        }
    }

    @Override
    public String toString() {
        return String.format("{%s, pageSize:%d, pageIndex:%d, totalCount:%d, currentPageRowCount:%d}", super.toString(), pageSize, pageIndex, totalCount, getCurrentPageRowCount());
    }
}
